#include "updatemessagestatusthread.h"
#include "../constantapp.h"
#include "../accountmanager.h"
#include "../sqlite/friendprofiletable.h"
#include "../sqlite/sqlitemanager.h"

#include <QDateTime>

UpdateMessageStatusThread::UpdateMessageStatusThread(QObject *parent) :
    QThread(parent)
{
}

void UpdateMessageStatusThread::pushMsgStatusFriendProfile(UserProfile* friendProfile) {
    sync.lock();
    m_queueFriendProfile.insert(0, friendProfile);
    sync.unlock();
    condition.wakeAll();
}

void UpdateMessageStatusThread::run()
{
    while (m_queueFriendProfile.length() != 0) {
        sync.lock();

        UserProfile* friendProfile = m_queueFriendProfile.last();

        int typeMsg = friendProfile->getTypeMsg();
        const qint64 fid = friendProfile->getUserId();
        const int senderMsg = friendProfile->getSenderMsg();
        const QString msgStatus = friendProfile->getMessageStatus();
        const qint64 time = friendProfile->getTime();

        Sqlite::FriendProfileTable::updateMessageStatus(fid,
                                                        senderMsg,
                                                        msgStatus,
                                                        time,
                                                        typeMsg);

        m_queueFriendProfile.removeLast();
        sync.unlock();
    }
}
